package com.bingxue.edu.teacher.model.resp;

import com.bingxue.edu.common.enums.JoinTypeEnum;
import com.bingxue.edu.management.education.model.entity.LessonStudent;
import com.bingxue.edu.management.education.model.resp.LessonStudentDetailResp;
import com.mybatisflex.annotation.RelationManyToOne;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * @author panbo
 * @since 2024/9/19
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "教师班级课次详情")
public class TeacherClassLessonDetailResp extends TeacherClassLessonResp {

    @Schema(description = "班级名称")
    @RelationManyToOne(selfField = "classId", targetTable = "edu_class_info", targetField = "id", valueField = "name")
    private String className;

    @Schema(description = "在读学生列表")
    private List<LessonStudentDetailResp> normalStudentList;

    @Schema(description = "转出学生列表")
    private List<LessonStudentDetailResp> changeClassOutStudentList;

    @Schema(description = "调出学生列表")
    private List<LessonStudentDetailResp> changeLessonOutStudentList;

    @Schema(description = "退课学生列表")
    private List<LessonStudentDetailResp> cancelStudentList;

    @Schema(description = "学生总人数")
    public Integer getTotalStudentCount() {
        return normalStudentList.size();
    }

    @Schema(description = "签到总人数")
    public Integer getAttendanceStudentCount() {
        return normalStudentList.stream().filter(LessonStudent::getSignInStatus).toList().size();
    }

    @Schema(description = "转入总人数")
    public Integer getChangeClassInStudentCount() {
        return normalStudentList.stream()
                .filter(item -> item.getStudentJoinType() == JoinTypeEnum.CHANGE_CLASS)
                .toList()
                .size();
    }

    @Schema(description = "调入总人数")
    public Integer getChangeLessonInStudentCount() {
        return normalStudentList.stream()
                .filter(item -> item.getStudentJoinType() == JoinTypeEnum.CHANGE_LESSON)
                .toList()
                .size();
    }

    @Schema(description = "转出总人数")
    public Integer getChangeClassOutStudentCount() {
        return changeClassOutStudentList.size();
    }

    @Schema(description = "调出总人数")
    public Integer getChangeLessonOutStudentCount() {
        return changeLessonOutStudentList.size();
    }

    @Schema(description = "退课总人数")
    public Integer getCancelStudentCount() {
        return cancelStudentList.size();
    }

    /**
     * 出勤率 = 实到人数/应到人数
     */
    @Schema(description = "出勤率")
    public Double getAttendanceRate() {
        if (getTotalStudentCount() == 0) {
            return 0.0;
        } else {
            return getAttendanceStudentCount() * 100.0 / getTotalStudentCount();
        }
    }
}
